package com.boc.ewr.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.Cac;
import com.boc.ewr.bcimp.entity.CassTlrMsg;
import com.boc.ewr.bcimp.entity.CorpEbkTlr;
import com.boc.ewr.bcimp.entity.HrEmpInfo;
import com.boc.ewr.bcimp.service.ICacService;
import com.boc.ewr.bcimp.service.ICassTlrMsgService;
import com.boc.ewr.bcimp.service.IHrEmpInfoService;
import com.boc.ewr.risk.entity.EmpBrkFdbk;
import com.boc.ewr.risk.entity.ExtranetCaFdbk;
import com.boc.ewr.risk.mapper.EmpBrkFdbkMapper;
import com.boc.ewr.risk.service.IEmpBrkFdbkService;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import com.boc.ewr.util.file.enumeration.ReturnChannelEnum;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 员工代客清单
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class EmpBrkFdbkServiceImpl extends ServiceImpl<EmpBrkFdbkMapper, EmpBrkFdbk> implements IEmpBrkFdbkService {

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IHrEmpInfoService hrEmpInfoService;

    @Autowired
    private ICacService cacService;

    /**
     * 导入人力资源提供的“员工信息
     * 将CAC中联系人手机号码与导入人力资源提供的“员工信息”手机号比对，如有相符的，提示对账信息中有员工手机号。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        // 清空当前记录
        //this.remove(null);

        // 生成数据
        List<EmpBrkFdbk> resultList = new ArrayList<>();
        // 获取员工List
        List<HrEmpInfo> hrEmpInfoList = hrEmpInfoService.list();
        for (HrEmpInfo hrEmpInfo : hrEmpInfoList) {
            String empMob = hrEmpInfo.getEmpMob();
            // 根据手机号对Cac做查询
            List<Cac> list = cacService.list(
                    new LambdaQueryWrapper<Cac>()
                            .eq(Cac::getLinkerTel, empMob).or()
                            .eq(Cac::getLinkerMobile, empMob).or()
                            .eq(Cac::getFinacePrincipalTel, empMob).or()
                            .eq(Cac::getFinacePrincipalMobile, empMob)
            );
            if (list.size()>0){
                EmpBrkFdbk empBrkFdbk = new EmpBrkFdbk();
                empBrkFdbk.setAdminOrgNo(
                        sysBaseAPI.selectAllById(
                                hrEmpInfo.getUnitCode()
                        ).getParentId()
                );
                empBrkFdbk.setBranchNo(hrEmpInfo.getUnitCode());
                empBrkFdbk.setBranchNm(hrEmpInfo.getUnitNm());
                empBrkFdbk.setEmpNm(hrEmpInfo.getEmpNm());
                empBrkFdbk.setEhrTlrNo(hrEmpInfo.getEmpCode());
                empBrkFdbk.setMobNum(hrEmpInfo.getEmpMob());

                resultList.add(empBrkFdbk);
            }
        }

        this.sendMsg(resultList);
        return this.saveBatch(resultList);
    }

    private void sendMsg(Collection<EmpBrkFdbk> entityList) {

        Set<String> orgSet = new HashSet();
        for (EmpBrkFdbk empBrkFdbk : entityList) {
            orgSet.add(empBrkFdbk.getBranchNo());
        }

        // 信息推送至网点对账人员
        String toUser = ToUserUtil.genToUser(orgSet);

        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "员工代客疑似数据",
                    "对账信息中存在员工手机号，请您及时查看并完成排查！",
                    MsgCategoryEnum.RISK_CHECK.getIndex());
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }

    }
}
